<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>innobackupex 备份 Xtrabackup 增量备份 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>innobackupex 备份 Xtrabackup 增量备份</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MySQL/">MySQL</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Xtrabackup中包含两个工具：</p>
<ul>
<li>xtrabackup - 用于热备份innodb, xtradb表的工具，不能备份其他表(MYISAM表)。</li>
<li>innobackupex - 对xtrabackup封装的perl脚本，提供了myisam表备份的能力。（能进行整库和数据表备份）。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：备份恢复之前请做好全库备份</p>
<h2 id="安装Xtrabackup"><a href="#安装Xtrabackup" class="headerlink" title="安装Xtrabackup"></a>安装Xtrabackup</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<a href="http://www.percona.com/doc/percona-xtrabackup/index.html" target="_blank" rel="external">官网网址</a></p>
<h3 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置文件中需要添加 <code>datadir = /usr/local/mysql/datadir</code> ，MYSQL数据文件目录</p>
<h3 id="1-自动安装yum源后，用yum安装"><a href="#1-自动安装yum源后，用yum安装" class="headerlink" title="1.自动安装yum源后，用yum安装"></a>1.自动安装yum源后，用yum安装</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">yum install -y gnupg</div><div class="line">rpm -Uhv http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x86_64.rpm</div><div class="line"> </div><div class="line">yum install -y percona-xtrabackup</div></pre></td></tr></table></figure>
<h3 id="2-手动写入yum源"><a href="#2-手动写入yum源" class="headerlink" title="2.手动写入yum源"></a>2.手动写入yum源</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;新建文件 <code>/etc/yum.repos.d/Percona.repo</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[percona]</div><div class="line">name = CentOS <span class="variable">$releasever</span> - Percona</div><div class="line">baseurl=http://repo.percona.com/centos/<span class="variable">$releasever</span>/os/<span class="variable">$basearch</span>/</div><div class="line">enabled = 1</div><div class="line">gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-percona</div><div class="line">gpgcheck = 1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;之后yum安装 ，安装后可执行 <code>xtrabackup -v</code> 查看</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;之后可以用xtrabackup 备份</p>
<h2 id="innobackupex-备份全库"><a href="#innobackupex-备份全库" class="headerlink" title="innobackupex 备份全库"></a>innobackupex 备份全库</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;备份主程序为 /usr/bin/innobackupex-1.5.1，其需要从 mysql 配置文件中读取相关信息，Mysql缺省配置文件 my.cnf 中未配置 datadir 选项，必须显性添加，否则备份程序会报错：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">1innobackupex:: Warning: Ignored unrecognized line 2 <span class="keyword">in</span> options : <span class="string">'xtrabackup: Error: Please set parameter '</span>datadir<span class="string">'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在Mysql 配置文件 <code>/etc/my.cnf</code> 配置文件添加 <code>datadir</code> 内容：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在[mysqld]段加入</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">datadir = /usr/<span class="built_in">local</span>/mysql/var</div></pre></td></tr></table></figure>
<h3 id="1-备份"><a href="#1-备份" class="headerlink" title="1.备份"></a>1.备份</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#/usr/bin/innobackupex-1.5.1 --user=root --password=password --defaults-file=/etc/my.cnf /usr/local/bbsBackup</span></div></pre></td></tr></table></figure>
<h3 id="2-恢复"><a href="#2-恢复" class="headerlink" title="2.恢复"></a>2.恢复</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">/usr/bin/innobackupex-1.5.1 --apply-log /usr/<span class="built_in">local</span>/bbsBackup/2011-09-26_02-00-01/</div><div class="line">/usr/bin/innobackupex-1.5.1 --copy-back /usr/<span class="built_in">local</span>/bbsBackup/2011-09-26_02-00-01/</div><div class="line">chown -R mysql:mysql /usr/<span class="built_in">local</span>/mysql/</div><div class="line">/etc/init.d/mysqld start</div></pre></td></tr></table></figure>
<h2 id="全量备份及恢复"><a href="#全量备份及恢复" class="headerlink" title="全量备份及恢复"></a>全量备份及恢复</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：使用xtrabackup，仅限InnoDB和xtradb表，且注意mysql配置文件my.cnf中需设置“<code>default_table_type = InnoDB</code>”否则不成功</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/usr/bin/xtrabackup --defaults-file=/etc/my.cnf --backup --target-dir=/usr/<span class="built_in">local</span>/bbsBackup/base/</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;恢复时执行两次：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">/usr/bin/xtrabackup --defaults-file=/etc/my.cnf --prepare --target-dir=/usr/<span class="built_in">local</span>/bbsBackup/base</div><div class="line">/usr/bin/xtrabackup --defaults-file=/etc/my.cnf --prepare --target-dir=/usr/<span class="built_in">local</span>/bbsBackup/base</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#将数据库停掉</span></div><div class="line">/etc/init.d/mysqld stop</div><div class="line"><span class="comment">#删除数据库目录下的ib*（ib开头的所有）文件。</span></div><div class="line">rm /usr/<span class="built_in">local</span>/mysql/var/ib*</div><div class="line"><span class="comment">#将/usr/local/bbsBackup/base目录下的ib*文件拷贝到数据库目录。</span></div><div class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/mysql/var/</div><div class="line">cp /usr/<span class="built_in">local</span>/bbsBackup/base/ib* ./</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置权限：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">chown mysql:mysql ib*</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启数据库后测试，是否成功。</p>
<h2 id="增量备份及恢复"><a href="#增量备份及恢复" class="headerlink" title="增量备份及恢复"></a>增量备份及恢复</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：做增量前当然要先进行全量备份，在全量的基础上来进行增量。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先进行全量备份。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/usr/bin/xtrabackup --defaults-file=/etc/my.cnf --backup --target-dir=/usr/<span class="built_in">local</span>/bbsBackup/base/</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在全量备份的基础上进行增量。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/usr/bin/xtrabackup --defaults-file=/etc/my.cnf --backup --target-dir=/usr/<span class="built_in">local</span>/bbsBackup/1 --incremental-basedir=/usr/<span class="built_in">local</span>/bbsBackup/base/</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：/usr/local/bbsBackup/1是每次都需修改的。比如第二次增量就改成/usr/local/bbsBackup/2增量恢复。（步骤同全量恢复，只是在执行恢复命令的时候中间多一步）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">/usr/bin/xtrabackup --defaults-file=/etc/my.cnf --prepare --target-dir=/usr/<span class="built_in">local</span>/bbsBackup/base</div><div class="line">/usr/bin/xtrabackup --target-dir=/usr/<span class="built_in">local</span>/bbsBackup/base --prepare --incremental-dir=/usr/<span class="built_in">local</span>/bbsBackup/1</div><div class="line">/usr/bin/xtrabackup --defaults-file=/etc/my.cnf --prepare --target-dir=/usr/<span class="built_in">local</span>/bbsBackup/base</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#将数据库停掉</span></div><div class="line">/etc/init.d/mysqld stop</div><div class="line"><span class="comment">#删除数据库目录下的ib*（ib开头的所有）文件。</span></div><div class="line">rm /usr/<span class="built_in">local</span>/mysql/var/ib*</div><div class="line"><span class="comment">#将/usr/local/bbsBackup/base目录下的ib*文件拷贝到数据库目录。</span></div><div class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/mysql/var/</div><div class="line">cp /usr/<span class="built_in">local</span>/bbsBackup/base/ib* ./</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设置权限：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">chown mysql:mysql ib*</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启数据库后测试，是否成功。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/MySQL/18. innobackupex 备份 Xtrabackup 增量备份/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
